(function() {
    'use strict';
    angular.module('weApp.controllers')
            //地图选点
            .controller('mapCtrl', ['$scope', '$rootScope', '$state', '$stateParams', '$timeout', '$http', 'weAppDataBase', '$ionicPopup', '$compile', 'showLinePageService', 'localStorageService', '$ionicLoading',
                function($scope, $rootScope, $state, $stateParams, $timeout, $http, weAppDataBase, $ionicPopup, $compile, showLinePageService, localStorageService, $ionicLoading) {
                    var height = 480;
                    if (screen.height) {
                        height = document.documentElement.clientHeight;
                    }

                    $('#showmap').css('height', height - 44 + 'px');

                    $scope.tag = $stateParams.tag;
                    $scope.tit = $stateParams.tit;
                    if ($scope.tit && $scope.tag) {
                        localStorageService.set("searchTag", JSON.stringify([$scope.tit, $scope.tag]));
                    } else {
                        var searchTag = JSON.parse(localStorageService.get("searchTag"));
                        $scope.tit = searchTag[0];
                        $scope.tag = searchTag[1];
                    }

                    if (!$rootScope.sAddr) {
                        var sAddr = JSON.parse(localStorageService.get("sAddr"));
                        $rootScope.sAddr = sAddr;
                    }

                    /*var he = null;
                     if (screen.height) {
                     he = screen.height - 175;
                     } else {
                     he = 480 - 175;
                     }
                     $('#showmap').css('height', he + 'px');*/

                    $scope.mCon = {
                        name: '',
                        addr: '',
                        location: ''
                    }

                    var map = new BMap.Map("showmap");    // 创建Map实例
                    var pn = new BMap.Point($rootScope.cityLocation.lng, $rootScope.cityLocation.lat);
                    map.centerAndZoom(pn, 17);     // 初始化地图,设置中心点坐标和地图级别
                    map.addControl(new BMap.NavigationControl());       // 添加平移缩放控件
                    map.enableScrollWheelZoom();    //启用滚轮放大缩小
                    var gc = new BMap.Geocoder();
                    var $tit = $('#m-tit');
                    var $addr = $('#m-addr');

                    $scope.fMap = function() {
                        // debugger;
                        // var p=map.getCenter();
//                        var p = new BMap.Point($rootScope.cityLocation.lng, $rootScope.cityLocation.lat);
                        gc.getLocation(pn, function(data) {
                            var tit = null;
                            var point = null;
                            var addr = null;
                            var surr = data.surroundingPois;
                            if (surr.length > 0) {
                                tit = surr[0].title;
                                point = surr[0].point;
                                addr = surr[0].address;
                                $tit.html(tit);
                                $addr.html(addr);
                                $scope.mCon = {
                                    name: tit,
                                    addr: addr,
                                    location: point
                                }
                            } else {
                                $scope.mCon.name = '';
                                $tit.html('');
                                $addr.html('');
                            }
                        }, {poiRadius: 500, numPois: 1});
                    }
                    $scope.fMap();

                    map.addEventListener("dragend", function(e) {
                        var p = map.getCenter();
                        gc.getLocation(p, function(data) {
                            var tit = null;
                            var point = null;
                            var addr = null;
                            var surr = data.surroundingPois;
                            if (surr.length > 0) {
                                tit = surr[0].title;
                                point = surr[0].point;
                                addr = surr[0].address;
                                $tit.html(tit);
                                $addr.html(addr);
                                $scope.mCon = {
                                    name: tit,
                                    addr: addr,
                                    location: point
                                }
                            } else {
                                $scope.mCon.name = '';
                                $tit.html('');
                                $addr.html('');
                            }
                        }, {poiRadius: 1000, numPois: 1});
                    });


                    $scope.cMap = function() {
                        // console.log('cMap');
                        if (!$scope.mCon.name) {
                            $ionicLoading.show({
                                template: '未检索到位置'
                            })

                            $timeout(function() {
                                $ionicLoading.hide();
                            }, 1000);
                            return false;
                        }
                        var data = $scope.mCon;
                        if ($scope.tag == 'sta') {
                            // $scope.searchContext.searchText = data.name;

                            if ($rootScope.serLoc) {
                                $rootScope.serLoc.sta = {name: data.name, location: data.location};
                                $rootScope.serLoc.sName = data.name;
                            } else {
                                $rootScope.serLoc = {
                                    sta: {name: data.name, location: data.location},
                                    sName: data.name
                                }
                            }
                            if ($rootScope.sAddr.end == "我的位置" && $rootScope.sAddr.addr) {
                                $rootScope.serLoc.end = $rootScope.sAddr.addr;
                                $rootScope.serLoc.eName = $rootScope.sAddr.addr.name;
                                $state.go('transfer', {data: $rootScope.serLoc});
                                return false;
                            }
                        } else if ($scope.tag == 'end') {
                            // $scope.searchContext.searchText = data.name;

                            if ($rootScope.serLoc) {
                                $rootScope.serLoc.end = {name: data.name, location: data.location};
                                $rootScope.serLoc.eName = data.name;
                            } else {
                                $rootScope.serLoc = {
                                    end: {name: data.name, location: data.location},
                                    eName: data.name
                                }
                            }
                            if ($rootScope.sAddr.sta == "我的位置" && $rootScope.sAddr.addr) {
                                $rootScope.serLoc.sta = $rootScope.sAddr.addr;
                                $rootScope.serLoc.sName = $rootScope.sAddr.addr.name;
                                $state.go('transfer', {data: $rootScope.serLoc});
                                return false;
                            }
                        }

                        if ($rootScope.serLoc.sName && $rootScope.serLoc.eName) {
                            if ($rootScope.serLoc.sName == $rootScope.serLoc.eName) {
                                $ionicLoading.show({
                                    template: '起点终点不能完全相同'
                                })

                                $timeout(function() {
                                    $ionicLoading.hide();
                                }, 2000);
                                return false;
                            } else {
                                $state.go('transfer', {data: $rootScope.serLoc});
                                return false;
                            }
                        } else {
                            $state.go('trans', {data: $rootScope.serLoc});
                        }
                    }
                }]);
})();